/**
 * Created by Administrator on 2017/12/7.
 */
$(function(){
  /*BEGIN 投票倒计时*/
/*  $.ajax({
    type: "get",
    async: false,
    dataType: 'json',
    url: '/v1/candidate/conf/info',
    data: {},
    "contentType": "application/json;charset=UTF-8",
    success: function(data){
      if(data.status == 200){
        var startTime = new Date(data.data.startTime).getTime();
        var endTime = new Date(data.data.endTime).getTime();
        var curTime = new Date().getTime();
        if(curTime < startTime){
          var clockStartTime = Math.floor((startTime - curTime)/1000);
          var clockDay,clockHour,clockMinute,clockSecond;
          var timer1 = setInterval(function(){
            clockStartTime --;
            clockDay = Math.floor(clockStartTime/86400);
            clockHour = Math.floor((clockStartTime%86400)/3600) < 10? '0' + Math.floor((clockStartTime%86400)/3600): Math.floor((clockStartTime%86400)/3600);
            clockMinute = Math.floor((clockStartTime%3600)/60) < 10? '0' + Math.floor((clockStartTime%3600)/60):Math.floor((clockStartTime%3600)/60);
            clockSecond = Math.floor(clockStartTime%60) < 10? '0' + Math.floor(clockStartTime%60): Math.floor(clockStartTime%60);
            $('.clock').html('<span>距离投票开始还有</span>' + '<i>' + clockDay + '</i>天' + '<i>' + clockHour + '</i>时' + '<i>' + clockMinute + '</i>分' + '<i>' + clockSecond + '</i>秒');
            if(clockStartTime <= 0){
              clearInterval(timer1);
              var clockEndTime = Math.floor((endTime - new Date().getTime())/1000);
              var timer2 = setInterval(function(){
                clockEndTime --;
                clockHour = Math.floor((clockEndTime%86400)/3600) < 10? '0' + Math.floor((clockEndTime%86400)/3600): Math.floor((clockEndTime%86400)/3600);
                clockMinute = Math.floor((clockEndTime%3600)/60) < 10? '0' + Math.floor((clockEndTime%3600)/60):Math.floor((clockEndTime%3600)/60);
                clockSecond = Math.floor(clockEndTime%60) < 10? '0' + Math.floor(clockEndTime%60): Math.floor(clockEndTime%60);
                $('.clock').html('<span>距离投票结束还有</span>' + '<i>' + clockHour + '</i>时' + '<i>' + clockMinute + '</i>分' + '<i>' + clockSecond + '</i>秒');
                if(clockEndTime <= 0){
                  clearInterval(timer2);
                  $('.clock').html('<span>投票已结束</span>');
                }
              },1000);
            }
          },1000);
        }else if(curTime > endTime){
          $('.clock').html('<span>投票已结束</span>');
        }else{
          var clockEndTime = Math.floor((endTime - curTime)/1000);
          var timer3 = setInterval(function(){
            clockEndTime --;
            clockHour = Math.floor((clockEndTime%86400)/3600) < 10? '0' + Math.floor((clockEndTime%86400)/3600): Math.floor((clockEndTime%86400)/3600);
            clockMinute = Math.floor((clockEndTime%3600)/60) < 10? '0' + Math.floor((clockEndTime%3600)/60):Math.floor((clockEndTime%3600)/60);
            clockSecond = Math.floor(clockEndTime%60) < 10? '0' + Math.floor(clockEndTime%60): Math.floor(clockEndTime%60);
            $('.clock').html('<span>距离投票结束还有</span>' + '<i>' + clockHour + '</i>时' + '<i>' + clockMinute + '</i>分' + '<i>' + clockSecond + '</i>秒');
            if(clockEndTime <= 0){
              clearInterval(timer3);
              $('.clock').html('<span>投票已结束</span>');
            }
          },1000);
        }
      }
    }
  });*/
  //暂时使用假数据代替，待删
  var data = {
    "msg": "ok",
    "data": {
      "startTime": "2018-1-28 09:00:00",
      "endTime": "2018-1-28 17:00:00"
    },
    "status": 200
  };
  if(data.status == 200){
    var startTime = new Date(data.data.startTime).getTime();
    var endTime = new Date(data.data.endTime).getTime();
    var curTime = new Date().getTime();
    if(curTime < startTime){
      var clockStartTime = Math.floor((startTime - curTime)/1000);
      var clockDay,clockHour,clockMinute,clockSecond;
      var timer1 = setInterval(function(){
        clockStartTime --;
        clockDay = Math.floor(clockStartTime/86400);
        clockHour = Math.floor((clockStartTime%86400)/3600) < 10? '0' + Math.floor((clockStartTime%86400)/3600): Math.floor((clockStartTime%86400)/3600);
        clockMinute = Math.floor((clockStartTime%3600)/60) < 10? '0' + Math.floor((clockStartTime%3600)/60):Math.floor((clockStartTime%3600)/60);
        clockSecond = Math.floor(clockStartTime%60) < 10? '0' + Math.floor(clockStartTime%60): Math.floor(clockStartTime%60);
        $('.clock').html('<span>距离投票开始还有</span>' + '<i>' + clockDay + '</i>天' + '<i>' + clockHour + '</i>时' + '<i>' + clockMinute + '</i>分' + '<i>' + clockSecond + '</i>秒');
        if(clockStartTime <= 0){
          clearInterval(timer1);
          var clockEndTime = Math.floor((endTime - new Date().getTime())/1000);
          var timer2 = setInterval(function(){
            clockEndTime --;
            clockHour = Math.floor((clockEndTime%86400)/3600) < 10? '0' + Math.floor((clockEndTime%86400)/3600): Math.floor((clockEndTime%86400)/3600);
            clockMinute = Math.floor((clockEndTime%3600)/60) < 10? '0' + Math.floor((clockEndTime%3600)/60):Math.floor((clockEndTime%3600)/60);
            clockSecond = Math.floor(clockEndTime%60) < 10? '0' + Math.floor(clockEndTime%60): Math.floor(clockEndTime%60);
            $('.clock').html('<span>距离投票结束还有</span>' + '<i>' + clockHour + '</i>时' + '<i>' + clockMinute + '</i>分' + '<i>' + clockSecond + '</i>秒');
            if(clockEndTime <= 0){
              clearInterval(timer2);
              $('.clock').html('<span>投票已结束</span>');
            }
          },1000);
        }
      },1000);
    }else if(curTime > endTime){
      $('.clock').html('<span>投票已结束</span>');
    }else{
      var clockEndTime = Math.floor((endTime - curTime)/1000);
      var timer3 = setInterval(function(){
        clockEndTime --;
        clockHour = Math.floor((clockEndTime%86400)/3600) < 10? '0' + Math.floor((clockEndTime%86400)/3600): Math.floor((clockEndTime%86400)/3600);
        clockMinute = Math.floor((clockEndTime%3600)/60) < 10? '0' + Math.floor((clockEndTime%3600)/60):Math.floor((clockEndTime%3600)/60);
        clockSecond = Math.floor(clockEndTime%60) < 10? '0' + Math.floor(clockEndTime%60): Math.floor(clockEndTime%60);
        $('.clock').html('<span>距离投票结束还有</span>' + '<i>' + clockHour + '</i>时' + '<i>' + clockMinute + '</i>分' + '<i>' + clockSecond + '</i>秒');
        if(clockEndTime <= 0){
          clearInterval(timer3);
          $('.clock').html('<span>投票已结束</span>');
        }
      },1000);
    }
  }
  /*END 投票倒计时*/

  /*BEGIN 设置最多选择10个人*/
  $(":checkbox").click(function(){
    if($(':checked').length >= 10){
      $(":checkbox").each(function(){
        if(!$(this)[0].checked) {
          $(this).attr("disabled", "disabled");
        }
        $('.msg').css('display','inline-block').text('最多选择10位员工');
        setTimeout(function(){
          $('.msg').css('display','none');
        },2000);
      });
    }else{
      $(":checkbox").each(function(){
        if(!$(this)[0].checked) {
          $(this).removeAttr("disabled");
        }
      });
    }
  });
  /*END 设置最多选择10个人*/

  /*BEGIN 点击提交*/
  $('.btn').click(function(){
    var voteDataList = [];
    $(':checked').each(function(){
      voteDataList.push($(this).val());
    });
    if(voteDataList.length == 0){
      $('.msg').css('display','inline-block').text('您未进行任何选择');
      setTimeout(function(){
        $('.msg').css('display','none');
      },2000);
      return;
    }
    if($('.voteBtn>input').val() == ''){
      $('.msg').css('display','inline-block').text('请输入您的工号');
      setTimeout(function(){
        $('.msg').css('display','none');
      },2000);
      return;
    }
    var empno = $('.voteBtn>input').val();
    var empnos = voteDataList.join(',');
    //发送提交请求
    console.log(empno);
    console.log(empnos);
    /*$.ajax({
      type: "post",
      async: false,
      dataType: 'json',
      url: '/v1/candidate/' + empno + '/vote',
      data: JSON.stringify({
        'empno': empno,
        'empnos': empnos
      }),
      "contentType": "application/json;charset=UTF-8",
      success: function(data){
        if(data.status == 200){
          $('.msg').css('display','inline-block').text('投票成功');
          setTimeout(function(){
            $('.msg').css('display','none');
          },2000);
        }else if(data.status == 401){
          $('.msg').css('display','inline-block').text('投票时间未开始');
          setTimeout(function(){
            $('.msg').css('display','none');
          },2000);
        }else if(data.status == 402){
          $('.msg').css('display','inline-block').text('投票已结束');
          setTimeout(function(){
            $('.msg').css('display','none');
          },2000);
        }else if(data.status == 403){
          $('.msg').css('display','inline-block').text('您已经投过票，请勿重复投票');
          setTimeout(function(){
            $('.msg').css('display','none');
          },2000);
        }else if(data.status == 404){
          $('.msg').css('display','inline-block').text('您输入的工号不存在，请重新输入');
          setTimeout(function(){
            $('.msg').css('display','none');
          },2000);
        }
      }
    });*/
  });
  /*END 点击提交*/
});